﻿using Nefarius.ViGEm.Client;
using Nefarius.ViGEm.Client.Targets;
using Nefarius.ViGEm.Client.Targets.Xbox360;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 摸鱼
{
    public class 手柄模拟
    {
        private ViGEmClient client;
        private IXbox360Controller controller;
        // 方向键
        public static readonly Xbox360Button Up = Xbox360Button.Up;
        public static readonly Xbox360Button Down = Xbox360Button.Down;
        public static readonly Xbox360Button Left = Xbox360Button.Left;
        public static readonly Xbox360Button Right = Xbox360Button.Right;

        // 功能键
        public static readonly Xbox360Button Start = Xbox360Button.Start;
        public static readonly Xbox360Button Back = Xbox360Button.Back;

        // 摇杆按键按下
        public static readonly Xbox360Button LS = Xbox360Button.LeftThumb;
        public static readonly Xbox360Button RS = Xbox360Button.RightThumb;

        // 肩键和扳机键
        public static readonly Xbox360Button LB = Xbox360Button.LeftShoulder;  // Left Bumper
        public static readonly Xbox360Button RB = Xbox360Button.RightShoulder; // Right Bumper
        //255为完全按下，0为松开
        public static readonly Xbox360Slider LT = Xbox360Slider.LeftTrigger;   // Left Trigger
        public static readonly Xbox360Slider RT = Xbox360Slider.RightTrigger;  // Right Trigger

        // 中心键
        public static readonly Xbox360Button Guide = Xbox360Button.Guide;

        // 主要动作键
        public static readonly Xbox360Button A = Xbox360Button.A;
        public static readonly Xbox360Button B = Xbox360Button.B;
        public static readonly Xbox360Button X = Xbox360Button.X;
        public static readonly Xbox360Button Y = Xbox360Button.Y;

        // 摇杆轴向（用于模拟摇杆移动）
        public static readonly Xbox360Axis LeftThumbX = Xbox360Axis.LeftThumbX;   //控制左右移动
        public static readonly Xbox360Axis LeftThumbY = Xbox360Axis.LeftThumbY;   //控制上下移动
        public static readonly Xbox360Axis RightThumbX = Xbox360Axis.RightThumbX;  //控制左右移动
        public static readonly Xbox360Axis RightThumbY = Xbox360Axis.RightThumbY;  //控制上下移动

        public 手柄模拟()
        {
            client = new ViGEmClient();
            controller = client.CreateXbox360Controller();
            controller.Connect();
        }
        public void PressAButton()
        {
            controller.SetButtonState(A, true);
            System.Threading.Thread.Sleep(100); // 短暂按下
            controller.SetButtonState(Xbox360Button.A, false);
        }
        public void ButtonDown(Xbox360Button xbox360Button) => controller.SetButtonState(xbox360Button, true);
        public void ButtonUp(Xbox360Button xbox360Button) => controller.SetButtonState(xbox360Button, false);

        /// <summary>
        /// 扳机键，255为完全按下，0为松开
        /// </summary>
        public void xbox360Slider(Xbox360Slider Xbox360Slider, byte value) => controller.SetSliderValue(Xbox360Slider, value);
        /// <summary>
        /// 摇杆轴向，value范围-32768到32767，0为摇杆居中
        /// </summary>
        public void xbox360Axis(Xbox360Axis Xbox360Axis, short value) => controller.SetAxisValue(Xbox360Axis, value);
        public void Dispose()
        {
            controller?.Disconnect();
            client?.Dispose();
        }
    }
}
